import { merge } from "lodash-es";
import { mapStore } from "../utils/store";
import { $http } from "../boot/http";

export default merge({
  namespaced: true,
  state: {},
  mutations: {},
  getters: {},
  actions: {
    loadSettings({ commit, dispatch }, params) {
      return dispatch(
        "toggleLoading",
        {
          key: "media.upload.setting",
          loading: async () => {
            const { data } = await $http.get(
              "/api/v1/media/upload/settings",
              params
            );
            return data;
          }
        },
        { root: true }
      );
    },
    async upload({ dispatch }, params) {
      return dispatch(
        "toggleLoading",
        {
          key: "media.upload",
          loading: () => {
            return new Promise((resolve, reject) =>
              dispatch("loadSettings")
                .then(settings => {
                  console.log(settings);
                  let fileName = params.tempFilePaths[0];
                  fileName = fileName
                    .substring(fileName.lastIndexOf("/") + 1)
                    .replace(/-/g, "");
                  let suffix = "";
                  if (params.tempFilePaths[0].name) {
                    let pos = params.tempFiles[0].name.lastIndexOf(".");
                    if (pos != -1) {
                      suffix = params.tempFiles[0].name.substring(pos);
                    }
                  }

                  fileName = fileName + suffix;
                  uni.uploadFile({
                    url: settings.url,
                    filePath: params.tempFilePaths[0],
                    fileType: "image",
                    name: "file",
                    formData: {
                      name: params.tempFilePaths[0],
                      key: fileName,
                      policy: settings.policy,
                      OSSAccessKeyId: settings.access_key,
                      success_action_status: "200",
                      //让服务端返回200,不然，默认会返回204
                      signature: settings.signature
                    },
                    success: res => {
                      let result = res;
                      result.url = settings.url + "/" + fileName;
                      // console.log(result.url)
                      resolve(result);
                    },
                    error: reject
                  });
                })
                .catch(reject)
            );
          }
        },
        { root: true }
      );
    },
    // 申述上传图片
    async reUpload({ dispatch }, { tempFilePaths }) {
      return dispatch(
        "toggleLoading",
        {
          key: "otc.upload",
          loading: () => {
            return new Promise((resolve, reject) =>
              dispatch("loadSettings")
                .then(settings => {
                  console.log(settings);
                  let fileName = tempFilePaths;
                  fileName = fileName
                    .substring(fileName.lastIndexOf("/") + 1)
                    .replace(/-/g, "");
                  console.log(fileName);
                  uni.uploadFile({
                    url: settings.url,
                    filePath: tempFilePaths,
                    fileType: "image",
                    name: "file",
                    formData: {
                      name: tempFilePaths,
                      key: fileName,
                      policy: settings.policy,
                      OSSAccessKeyId: settings.access_key,
                      success_action_status: "200",
                      //让服务端返回200,不然，默认会返回204
                      signature: settings.signature
                    },
                    success: res => {
                      let result = res;
                      result.url = settings.url + "/" + fileName;
                      console.log(result);
                      resolve(result);
                    },
                    error: reject
                  });
                })
                .catch(reject)
            );
          }
        },
        { root: true }
      );
    },
  }
});
